--- |
  ; RUN: llc %s -march=x86-64 -run-pass=livedebugvalues -o - -experimental-debug-variable-locations |  FileCheck %s -implicit-check-not=DBG_VALUE
  ;
  ; Test that InstrRefBasedLDV / livedebugvalues doesn't crash when it sees
  ; illegal instruction referencing debug-info. This can be caused by
  ; optimisations not updating debug-info or doing it incorrectly, which is
  ; inevitable. When that happens, we should safely drop the variable location,
  ; but not crash.

  define i32 @_Z8bb_to_bb() local_unnamed_addr !dbg !12 {
  entry:
    ret i32 0, !dbg !17
  }

  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!7, !8, !9, !10}
  !llvm.ident = !{!11}
  !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 10.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, debugInfoForProfiling: true, nameTableKind: None)
  !1 = !DIFile(filename: "main.cpp", directory: "F:\")
  !2 = !{}
  !3 = !{!4}
  !4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
  !5 = distinct !DIGlobalVariable(name: "start", scope: !0, file: !1, line: 4, type: !6, isLocal: false, isDefinition: true)
  !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !7 = !{i32 2, !"Dwarf Version", i32 4}
  !8 = !{i32 2, !"Debug Info Version", i32 3}
  !9 = !{i32 1, !"wchar_size", i32 2}
  !10 = !{i32 7, !"PIC Level", i32 2}
  !11 = !{!"clang version 10.0.0"}
  !12 = distinct !DISubprogram(name: "bb_to_bb", linkageName: "bb_to_bb", scope: !1, file: !1, line: 6, type: !13, scopeLine: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !15)
  !13 = !DISubroutineType(types: !14)
  !14 = !{!6, !6}
  !15 = !{!16}
  !16 = !DILocalVariable(name: "myVar", scope: !12, file: !1, line: 7, type: !6)
  !17 = !DILocation(line: 10, scope: !12)

...
---
name: _Z8bb_to_bb
debugInstrRef: true
debugValueSubstitutions:
  - { srcinst: 4, srcop: 0, dstinst: 3, dstop: 0, subreg: 0 }
body:  |
  bb.0.entry:
    successors: %bb.1, %bb.2
    ; CHECK-LABE: bb.0.entry:

    $rax = MOV64ri 1, debug-instr-number 1, debug-location !17
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0), debug-location !17
    ;; First check that picking out location works as usual.
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(1, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $rax

    $rax = MOV64ri 1, debug-instr-number 2, debug-location !17
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(2, 999), debug-location !17
    ;; Test out of bounds operand number.
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(2, 999)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    $rax = MOV64ri 1, debug-instr-number 3, debug-location !17
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(3, 1), debug-location !17
    ;; Test non-register operand
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(3, 1)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    ;; FIXME: We should test what happens when this meta-instruction is seen
    ;; by livedbugvalues with an instruction number. However, right now it's
    ;; impossible to turn the machine-code verifier off when loading MIR?
    ;KILL implicit killed $eflags, debug-instr-number 4, debug-location !17
    ;DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(4, 0), debug-location !17
    ;;; Test non-def operand
    ;; check:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(4, 0)
    ;; check-next: DBG_VALUE_LIST {{.+}}, $noreg

    $noreg = MOV32ri 1, debug-instr-number 5, debug-location !17
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(5, 0), debug-location !17
    ;; Def of $noreg?
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(5, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    JCC_1 %bb.1, 1, implicit $eflags
    JMP_1 %bb.2

  bb.1:
    successors: %bb.3
    ; CHECK-LABEL: bb.1:

    DBG_PHI $rax, 6
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(6, 1), debug-location !17
    ;; Test out-of-bounds reference to a DBG_PHI.
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(6, 1)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    DBG_PHI $noreg, 7
    JMP_1 %bb.3

  bb.2:
    successors: %bb.3
    ; CHECK-LABEL: bb.2:
    DBG_PHI 1, 6
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(6, 0), debug-location !17
    ;; Test non-reg operand to DBG_PHI. It's not clear if this can ever happen
    ;; as the result of an optimisation, but lets test for it anyway.
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(6, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    DBG_PHI 1, 7
    JMP_1 %bb.3

  bb.3:
    ; CHECK-LABEL: bb.3:
    DBG_INSTR_REF !16, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(7, 0), debug-location !17
    ;; PHI resolution of illegal inputs shouldn't crash either. It should also
    ;; come out as a $noreg location.
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(7, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}}, $noreg

    RET 0, debug-location !17

...
